﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;
using QuanLyHopDong.Web.Common;
using Resources;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class New : System.Web.UI.Page
    {
        #region Properties

        private HopDongBiz _hopDongBiz;
        private HopDongBiz HopDongManager
        {
            get { return _hopDongBiz ?? (_hopDongBiz = new HopDongBiz()); }
        }

        private EmployeeBiz _employeeBiz;
        private EmployeeBiz EmployeeManager { get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); } }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadEmployees();
                txtNgayKy.Text = DateTime.Today.ToString(GlobalStrings.DateFormat);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            #region Collect input

            var newHopDong = new Biz.Model.HopDong
                             {
                                 SoHopDong = txtSoHopDong.Text.Trim(),
                                 TenHopDong = txtTenHopDong.Text.Trim(),
                                 LoaiDuAn = txtLoaiDuAn.Text.Trim(),
                                 CreatedDate = DateTime.Now,
                                 ModifiedDate = DateTime.Now,
                                 CreatedBy = WebCommon.CurrrentEmployee.Id,
                                 ModifiedBy = WebCommon.CurrrentEmployee.Id,
                                 TenDonVi = txtTenDonVi.Text.Trim(),
                                 EmployeeId = int.Parse(ddlEmployees.SelectedValue)
                             };

            DateTime ngayKy;
            if (DateTime.TryParseExact(txtNgayKy.Text.Trim(), GlobalStrings.DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out ngayKy))
            {
                newHopDong.NgayKy = ngayKy;
            }

            DateTime ngayBatDauThiCong;
            if (DateTime.TryParseExact(txtNgayBatDauThiCong.Text.Trim(), GlobalStrings.DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out ngayBatDauThiCong))
            {
                newHopDong.NgayBatDauThiCong = ngayBatDauThiCong;
            }

            DateTime ngayKetThucThiCong;
            if (DateTime.TryParseExact(txtNgayKetThucThiCong.Text.Trim(), GlobalStrings.DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out ngayKetThucThiCong))
            {
                newHopDong.NgayKetThucThiCong = ngayKetThucThiCong;
            }

            DateTime ngayHoanThien;
            if (DateTime.TryParseExact(txtNgayHoanThien.Text.Trim(), GlobalStrings.DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out ngayHoanThien))
            {
                newHopDong.NgayHoanThien = ngayHoanThien;
            }

            decimal giaTriHopDongChuaVat;
            if (decimal.TryParse(txtGiaTriHopDongChuaVAT.Text.Trim(), out giaTriHopDongChuaVat))
            {
                newHopDong.GiaTriHopDongChuaVAT = giaTriHopDongChuaVat;
            }

            decimal vat;
            if (decimal.TryParse(txtVat.Text.Trim(), out vat))
            {
                newHopDong.VAT = vat;
            }

            decimal giaTriVatTu;
            if (decimal.TryParse(txtGiaTriVatTu.Text.Trim(), out giaTriVatTu))
            {
                newHopDong.GiaTriVatTu = giaTriVatTu;
            }

            decimal giaTriNhanCong;
            if (decimal.TryParse(txtGiaTriNhanCong.Text.Trim(), out giaTriNhanCong))
            {
                newHopDong.GiaTriNhanCong = giaTriNhanCong;
            }

            decimal chiPhiKhac;
            if (decimal.TryParse(txtChiPhiKhac.Text.Trim(), out chiPhiKhac))
            {
                newHopDong.ChiPhiKhac = chiPhiKhac;
            }

            decimal tongGiaTriHopDong;
            if (decimal.TryParse(txtTongGiaTriHopDong.Text.Trim(), out tongGiaTriHopDong))
            {
                newHopDong.TongGiaTriHopDong = tongGiaTriHopDong;
            }

            // files
            var hopDongDocs = new List<HopDongDoc>();

            // file 1
            //var fileName = string.Empty;
            if (fileHopDong.HasFile)
            {
                hopDongDocs.Add(new HopDongDoc
                {
                    FileHopDong = Path.GetFileName(fileHopDong.PostedFile.FileName)
                });
                //fileName = Path.GetFileName(fileHopDong.PostedFile.FileName);
                //newHopDong.FileHopDong = fileName;
            }
            if (fileHopDong2.HasFile)
            {
                hopDongDocs.Add(new HopDongDoc
                {
                    FileHopDong = Path.GetFileName(fileHopDong2.PostedFile.FileName)
                });
            }
            if (fileHopDong3.HasFile)
            {
                hopDongDocs.Add(new HopDongDoc
                {
                    FileHopDong = Path.GetFileName(fileHopDong3.PostedFile.FileName)
                });
            }
            if (fileHopDong4.HasFile)
            {
                hopDongDocs.Add(new HopDongDoc
                {
                    FileHopDong = Path.GetFileName(fileHopDong4.PostedFile.FileName)
                });
            }

            newHopDong.HopDongDocs = hopDongDocs;

            newHopDong.TrangThai = int.Parse(ddlTrangThaiHopDong.SelectedValue);

            #endregion

            // create hopdong
            try
            {
                HopDongManager.CreateHopDong(newHopDong);
            }
            catch (ApplicationException appEx)
            {
                lblErrorMessage.Text = WebCommon.GetGlobalString(appEx.Message);
                return;
            }

            // upload files
            WebCommon.UploadFile(newHopDong.Id, fileHopDong);
            WebCommon.UploadFile(newHopDong.Id, fileHopDong2);
            WebCommon.UploadFile(newHopDong.Id, fileHopDong3);
            WebCommon.UploadFile(newHopDong.Id, fileHopDong4);
            //if (fileHopDong.HasFile)
            //{
            //    var folderPath = Server.MapPath(string.Format("~/Files/HopDong/{0}", newHopDong.Id));
            //    // create folder
            //    if (!Directory.Exists(folderPath))
            //        Directory.CreateDirectory(folderPath);

            //    // save file
            //    var filePathFull = folderPath + "/" + fileName;
            //    if(File.Exists(filePathFull))
            //        File.Delete(filePathFull);
            //    fileHopDong.PostedFile.SaveAs(filePathFull);
            //}

            Response.Redirect("Edit.aspx?id=" + newHopDong.Id);
        }

        #endregion

        #region Helpers

        private void LoadEmployees()
        {
            var employees = EmployeeManager.GetAllEmployees();
            ddlEmployees.DataValueField = "Id";
            ddlEmployees.DataTextField = "Name";
            ddlEmployees.DataSource = employees;
            ddlEmployees.DataBind();
        }

        #endregion
    }
}